<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('公寓信息列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <!-- <label>公寓所在城市：</label>
                            <select name="addressCity" id="citySelect" th:with="type=${@dict.getType('address_city')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select> -->
                            <label>公寓所在城市：</label>
                            <input type="text" name="addressCity" id="citySelect" class="form-control">
                        </li>
                        <li>
                            <label>公寓所在社区：</label>
                            <select name="addressNeighborhood" id="neighborhoodSelect">
                                <option value="">所有</option>
                            </select>
                        </li>
                        <li>
                            <label>公寓所在小区：</label>
                            <select name="addressCommunity" id="communitySelect">
                                <option value="">所有</option>
                            </select>
                        </li>
                        <li>
                            <label>公寓户型：</label>
                            <select name="apartmentType" th:with="type=${@dict.getType('apartment_type')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>公寓状态：</label>
                            <select name="status" th:with="type=${@dict.getType('apartment_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="apartment:apartmentInfo:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="apartment:apartmentInfo:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="apartment:apartmentInfo:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="apartment:apartmentInfo:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('apartment:apartmentInfo:edit')}]];
    var removeFlag = [[${@permission.hasPermi('apartment:apartmentInfo:remove')}]];
    var addressCityDatas = [[${@dict.getType('address_city')}]];
    var apartmentTypeDatas = [[${@dict.getType('apartment_type')}]];
    var statusDatas = [[${@dict.getType('apartment_status')}]];
    var prefix = ctx + "apartment/apartmentInfo";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "公寓信息",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '公寓ID'
                },
                {
                    field: 'addressProvince',
                    title: '公寓所在省份'
                },
                {
                    field: 'addressCity',
                    title: '公寓所在城市',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(addressCityDatas, value);
                    }
                },
                {
                    field: 'addressNeighborhood',
                    title: '公寓所在社区'
                },
                {
                    field: 'addressCommunity',
                    title: '公寓所在小区'
                },
                {
                    field: 'unit',
                    title: '公寓单元'
                },
                {
                    field: 'doorNumber',
                    title: '公寓门牌号'
                },
                {
                    field: 'apartmentType',
                    title: '公寓户型',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(apartmentTypeDatas, value);
                    }
                },
                {
                    field: 'rent',
                    title: '租金',
                    formatter: function(value, row, index) {
                        return '￥' + parseFloat(value).toFixed(2);
                    }
                },
                {
                    field: 'status',
                    title: '公寓状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    document.addEventListener('DOMContentLoaded', function () {
        const citySelect = document.getElementById('citySelect');
        const neighborhoodSelect = document.getElementById('neighborhoodSelect');
        const communitySelect = document.getElementById('communitySelect');

        citySelect.addEventListener('change', function () {
            const selectedCity = citySelect.value;
            if (selectedCity === '') {
                // 清空社区和小区下拉框
                neighborhoodSelect.innerHTML = '<option value="">所有</option>';
                communitySelect.innerHTML = '<option value="">所有</option>';
            } else {
                fetchNeighborhoods(selectedCity);
            }
        });

        neighborhoodSelect.addEventListener('change', function () {
            const selectedCity = citySelect.value;
            const selectedNeighborhood = neighborhoodSelect.value;
            if (selectedNeighborhood === '') {
                // 清空小区下拉框
                communitySelect.innerHTML = '<option value="">所有</option>';
            } else {
                fetchCommunities(selectedCity, selectedNeighborhood);
            }
        });

        function fetchNeighborhoods(city) {
            fetch(`/apartment/apartmentInfo/neighborhoods?city=${city}`)
                .then(response => response.json())
                .then(responseData => {
                    if (responseData.code === 0 && Array.isArray(responseData.data)) {
                        let options = '<option value="">所有</option>';
                        responseData.data.forEach(neighborhood => {
                            options += `<option value="${neighborhood}">${neighborhood}</option>`;
                        });
                        neighborhoodSelect.innerHTML = options;
                        communitySelect.innerHTML = '<option value="">所有</option>';
                    } else {
                        console.error('Unexpected response format:', responseData);
                    }
                })
                .catch(error => console.error('Error:', error));
        }

        function fetchCommunities(city, neighborhood) {
            fetch(`/apartment/apartmentInfo/communities?city=${city}&neighborhood=${neighborhood}`)
                .then(response => response.json())
                .then(responseData => {
                    if (responseData.code === 0 && Array.isArray(responseData.data)) {
                        let options = '<option value="">所有</option>';
                        responseData.data.forEach(community => {
                            options += `<option value="${community}">${community}</option>`;
                        });
                        communitySelect.innerHTML = options;
                    } else {
                        console.error('Unexpected response format:', responseData);
                    }
            })
            .catch(error => console.error('Error:', error));
        }
    });
</script>
</body>
</html>